package com.ruoyi.business.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.business.domain.Beds;
import com.ruoyi.business.domain.model.AccommodationInfo;
import com.ruoyi.business.service.IBedsPlusService;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.business.domain.bo.DormitoryBo;
import com.ruoyi.business.domain.vo.DormitoryVo;
import com.ruoyi.business.domain.Dormitory;
import com.ruoyi.business.mapper.DormitoryMapper;
import com.ruoyi.business.service.IDormitoryService;

import java.util.List;
import java.util.Collection;

/**
 * 宿舍Service业务层处理
 *
 * @author ruoyi
 * @date 2023-06-25
 */
@RequiredArgsConstructor
@Service
public class DormitoryServiceImpl implements IDormitoryService {

    private final DormitoryMapper dormitoryMapper;
    private final IBedsPlusService bedsPlusService;

    /**
     * 查询宿舍
     */
    @Override
    public DormitoryVo queryById(Long id) {
        return dormitoryMapper.selectVoById(id);
    }

    /**
     * 查询宿舍列表
     */
    @Override
    public TableDataInfo<DormitoryVo> queryPageList(DormitoryBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<Dormitory> lqw = buildQueryWrapper(bo);
        Page<DormitoryVo> result = dormitoryMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询宿舍列表
     */
    @Override
    public List<DormitoryVo> queryList(DormitoryBo bo) {
        LambdaQueryWrapper<Dormitory> lqw = buildQueryWrapper(bo);
        return dormitoryMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<Dormitory> buildQueryWrapper(DormitoryBo bo) {
        LambdaQueryWrapper<Dormitory> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getDormitoryId()), Dormitory::getDormitoryId, bo.getDormitoryId());
        lqw.eq(StringUtils.isNotBlank(bo.getOutsource()), Dormitory::getOutsource, bo.getOutsource());

        if (bo.getNature()!=null){
//            if (bo.getNature().equals("招待所")){
//                lqw.eq(StringUtils.isNotBlank(bo.getNature()), Dormitory::getNature, bo.getNature());
//            }
            lqw.eq(StringUtils.isNotBlank(bo.getNature()), Dormitory::getNature, bo.getNature());
//            else {
//                String[] strings={"宿舍","夫妻","大学生"};
//                lqw.in(Dormitory::getNature, strings);
//            }
        }
        if (bo.getBoolmanyuan()!=null){
            if (bo.getBoolmanyuan().equals("未满")){
                lqw.apply("capacity != current_capacity");
            }
            else if (bo.getBoolmanyuan().equals("满员")){
                lqw.apply("capacity = current_capacity and current_capacity!=0");
            }
            else if (bo.getBoolmanyuan().equals("空房")){
                lqw.apply("current_capacity = 0");
            }
        }
        lqw.like(StringUtils.isNotBlank(bo.getBuildingName()), Dormitory::getBuildingName, bo.getBuildingName());
        lqw.orderByAsc(Dormitory::getBuildingName).orderByAsc(Dormitory::getFloor).orderByAsc(Dormitory::getDormitoryId);
        return lqw;
    }

    /**
     * 新增宿舍
     */
    @Override
    public Boolean insertByBo(DormitoryBo bo) {
        Dormitory add = BeanUtil.toBean(bo, Dormitory.class);
        validEntityBeforeSave(add);
        boolean flag = dormitoryMapper.insert(add) > 0;
        return flag;
    }

    /**
     * 新增宿舍
     */
    @Override
    public Dormitory insertByBo2(DormitoryBo bo) {
        Dormitory add = BeanUtil.toBean(bo, Dormitory.class);
        validEntityBeforeSave(add);
        boolean flag = dormitoryMapper.insert(add) > 0;
        if (flag) {
            return add;
        }
        return null;
    }

    /**
     * 修改宿舍
     */
    @Override
    public Boolean updateByBo(DormitoryBo bo) {
        Dormitory update = BeanUtil.toBean(bo, Dormitory.class);
        validEntityBeforeSave(update);
        return dormitoryMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(Dormitory entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除宿舍
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 如果宿舍下有床位，且床位上有人居住就不能删除宿舍
            LambdaQueryWrapper<Beds> bedsLambdaQueryWrapper = new LambdaQueryWrapper();
            bedsLambdaQueryWrapper.in(Beds::getDormitoryId, ids);
            bedsLambdaQueryWrapper.eq(Beds::getStatus, "已占位");
            int size = bedsPlusService.list(bedsLambdaQueryWrapper).size();
            if (size > 0) {
                return false;
            }
        }
        return dormitoryMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public List<AccommodationInfo> getAccommodationInfoList(DormitoryBo bo) {
        return dormitoryMapper.getAccommodationInfoList(bo);
    }

    @Override
    public TableDataInfo<DormitoryVo> getDormitoryIListByIds(List<Long> ids) {
        return TableDataInfo.build(dormitoryMapper.getDormitoryIListByIds(ids));
        //return dormitoryMapper.getDormitoryIListByIds(ids);
    }

}
